package controllers

import (
	"encoding/json"
	"fmt"
	"mytools/devtool"
	"strings"

	"github.com/astaxie/beego"
)

type JsonController struct {
	beego.Controller
}

func (c *JsonController) Get() {
	c.Data["Website"] = "beego.me"
	c.Data["Email"] = "astaxie@gmail.com"
	c.TplName = "testjson.html"
}

func (c *JsonController) Post() {
	jsonstr := c.GetString("jsonstr")
	tags := c.GetString("tags")
	c.Data["jsonstr"] = jsonstr
	c.Data["tags"] = tags

	var dat map[string]interface{}
	modelresult := jsonstr
	if err := json.Unmarshal([]byte(jsonstr), &dat); err != nil {
		fmt.Printf("Unmarshal err, %v\n", err)

	} else {
		modelresult, _ = devtool.JsonToModel(dat, "")

		c.Data["modelresult"] = modelresult
		fmt.Println(modelresult)
	}
	if len(tags) > 0 {
		tagsArr := strings.Split(tags, ",")
		if len(tagsArr) > 0 {
			modelresult = devtool.AddJsonFormGormTagName(modelresult, tagsArr)
			c.Data["modelresult"] = modelresult

		}
	}

	// c.TplName = "index.tpl"
	// s := c.Ctx.Input.RequestBody
	// body, _ := ioutil.ReadAll(s)

	c.TplName = "testjson.html"

}
